
package com.sg.fileService.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.springframework.stereotype.Component;

import com.sg.fileService.common.ErrorCode;
import com.sg.fileService.common.FileUploadException;
import com.sg.fileService.entity.FileTypeConfig;

import lombok.extern.slf4j.Slf4j;

/**  
 * ClassName:Property <br/>  
 * Function: 通用上传servelet静态配置类 <br/>  
 * Reason:   存储上传工具参数 <br/>  
 * Date:     2017年12月26日 上午10:02:26 <br/>  
 * @author   Lwt  
 * @version  v0.0.1
 * @since    JDK 1.8  
 * @see        
 */
@Slf4j
@Component
public class Property {
	
	/**
	 * 
	 * Creates a new instance of Property.  
	 * 通过spring创建实例时加载配置文件
	 * @throws FileUploadException
	 */
	public Property() throws FileUploadException {
		readProperty();
	}
	
	/**
	 * 
	 * readProperty:读取配置文件 <br/>  
	 *  
	 * @author Lwt  
	 * @throws FileUploadException  
	 * @since JDK 1.8
	 */
	private void readProperty() throws FileUploadException {
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("fileUploadConfig.properties");
		Properties props = new Properties();
		try {
			props.load(in);
		} catch (IOException e) {
			log.error(ErrorCode.CONFIG_NOT_EXIST.getErrorCodeValue() + " : " + ErrorCode.CONFIG_NOT_EXIST.getCodeMessage());
		}
		
		try {
			loadProperty(props);
		} catch (FileUploadException e) {
			log.error(e.getErrorCode() + " : " + e.getErrorMessage());
		}
	}
	
	/**
	 * 
	 * loadProperty: 将配置文件加载并整理到Property类中 <br/>  
	 * @author Lwt  
	 * @param props 配置文件对象
	 * @throws FileUploadException  
	 * @since JDK 1.8
	 */
	private void loadProperty(Properties props) throws FileUploadException {
		
		Property.BASE_PATH = props.getProperty("basePath");
		Property.DEFAULT_PATH = props.getProperty("defaultPath");
		Property.DATEPATH_EXPRESSION = props.getProperty("datePathExpression");
		Property.GLOBAL_RENAME_EXPRESSION = props.getProperty("renameExpression");
		Property.GLOBAL_RENAME_ENABLED = Boolean.valueOf(props.getProperty("renameEnabled"));
		
		Property.V_MINI_WIDTH = Integer.valueOf(props.getProperty("video.encoder.mini.width"));
		Property.V_MINI_HEIGHT = Integer.valueOf(props.getProperty("video.encoder.mini.height"));
		Property.V_MIDDLE_WIDTH = Integer.valueOf(props.getProperty("video.encoder.middle.width"));
		Property.V_MIDDLE_HEIGHT = Integer.valueOf(props.getProperty("video.encoder.middle.height"));
		Property.V_BIG_WIDTH = Integer.valueOf(props.getProperty("video.encoder.big.width"));
		Property.V_BIG_HEIGHT = Integer.valueOf(props.getProperty("video.encoder.big.height"));
		
		Property.IMG_BREVIARY_WIDTH = Integer.valueOf(props.getProperty("image.breviary.width"));
		Property.IMG_BREVIARY_HEIGHT = Integer.valueOf(props.getProperty("image.breviary.height"));
		Property.IMG_SMALL_WIDTH = Integer.valueOf(props.getProperty("image.small.width"));
		Property.IMG_SMALL_HEIGHT = Integer.valueOf(props.getProperty("image.small.height"));
		Property.IMG_MIDDLE_WIDTH = Integer.valueOf(props.getProperty("image.middle.width"));
		Property.IMG_MIDDLE_HEIGHT = Integer.valueOf(props.getProperty("image.middle.height"));
		Property.IMG_BIG_WIDTH = Integer.valueOf(props.getProperty("image.big.width"));
		Property.IMG_BIG_HEIGHT = Integer.valueOf(props.getProperty("image.big.height"));
		
		
		Property.LAST_PATH_O = props.getProperty("lastPath.original");
		Property.LAST_PATH_T = props.getProperty("lastPath.breviary");
		Property.LAST_PATH_S = props.getProperty("lastPath.small");
		Property.LAST_PATH_M = props.getProperty("lastPath.middle");
		Property.LAST_PATH_L = props.getProperty("lastPath.large");		
		
		props.setProperty("video.format.type",Property.VIDEO_FORMAT_TYPE );	
		props.setProperty("audio.format.type",Property.AUDIO_FORMAT_TYPE);
		props.setProperty("image.format.type",Property.IMG_FORMAT_TYPE);	
		
		Property.FILE_IN_DB = Boolean.valueOf(props.getProperty("fileInDB"));
		String typeListStr = props.getProperty("fileTypes");
		Property.FILE_TYPE_LIST = typeListStr == null || typeListStr.length() == 0 ? null : 
			Arrays.asList(typeListStr.split(","));
		Property.TYPE_CONFIG = new HashMap<String, FileTypeConfig>();
		
		//开始将自定义文件路径加载到配置表中
		FileTypeConfig config;
		String expression;
		String path;
		String formatType;
		
		for (String typeName : FILE_TYPE_LIST) {
			config = new FileTypeConfig();
			config.setEncodeAble(Boolean.valueOf(props.getProperty(typeName+".encoder.enable")));
			config.setRenameEnabled(Boolean.valueOf(props.getProperty(typeName + ".renameEnabled", props.getProperty("renameEnabled"))));
			config.setRenameExpression(props.getProperty(typeName + ".renameExpression", props.getProperty("renameExpression")));
			expression = props.getProperty(typeName + ".expression");
			path = props.getProperty(typeName + ".path", Property.DEFAULT_PATH);
			formatType=props.getProperty(typeName+".format.type");
			/**
			 * 自定义文件路径的识别公式不能为空, 如果识别不了将送往默认文件路径下
			 */
			if (expression == null || expression.length() == 0) {
				throw new FileUploadException(ErrorCode.EXPRESSION_NULL, null);
			}
			
			
			config.setTypeExpression(expression);
			config.setTypePath(path);
			config.setTypeName(typeName);
			config.setFormatType(formatType);
			Property.TYPE_CONFIG.put(typeName, config);
		}
	}
	
	/**
	 * 基础文件路径
	 */
	public static String BASE_PATH;
	/**
	 * 日期路径模板
	 */
	public static String DATEPATH_EXPRESSION;
	
	/**
	 * 默认文件路径
	 */
	public static String DEFAULT_PATH;
	
	/**
	 * 重命名公式
	 */
	public static String GLOBAL_RENAME_EXPRESSION;
	
	/**
	 * 是否启用重命名
	 */
	public static Boolean GLOBAL_RENAME_ENABLED;
	
	/**
	 * 文件类型列表
	 */
	public static List<String> FILE_TYPE_LIST;
	
	/**
	 * 文件类型配置
	 */
	public static Map<String, FileTypeConfig> TYPE_CONFIG;
	
	/**
	 * 原始文件存放文件夹名称
	 */
	public static String LAST_PATH_O;
	/**
	 * 缩略语文件存放文件夹名称
	 */
	public static String LAST_PATH_T;
	/**
	 * 压缩小文件存放文件夹名称
	 */
	public static String LAST_PATH_S;
	/**
	 * 压缩中等尺寸文件存放文件夹名称
	 */
	public static String LAST_PATH_M;
	/**
	 * 压缩大尺寸文件存放文件夹名称
	 */
	public static String LAST_PATH_L;
	
	/**
	 * 缩略图宽
	 */
	public static int IMG_BREVIARY_WIDTH;

	/**
	 * 缩略图高
	 */
	public static int IMG_BREVIARY_HEIGHT;

	/**
	 * 小图图宽
	 */
	public static int IMG_SMALL_WIDTH;

	/**
	 * 小图图高
	 */
	public static int IMG_SMALL_HEIGHT;
	/**
	 * 中等图图宽
	 */
	public static int IMG_MIDDLE_WIDTH;

	/**
	 * 中等图图高
	 */
	public static int IMG_MIDDLE_HEIGHT;
	/**
	 * 大图图宽
	 */
	public static int IMG_BIG_WIDTH;

	/**
	 * 大图图高
	 */
	public static int IMG_BIG_HEIGHT;
	
	/**
	 * 数据是否入库
	 */
	public static boolean FILE_IN_DB;
	
	/**
	 * 视频转换最小尺寸宽度
	 */
	public static int V_MINI_WIDTH;
	/**
	 * 视频转换最小尺寸高度
	 */
	public static int V_MINI_HEIGHT;
	/**
	 * 视频转换中等尺寸宽度
	 */
	public static int V_MIDDLE_WIDTH;
	/**
	 * 视频转换中等尺寸高度
	 */
	public static int V_MIDDLE_HEIGHT;
	/**
	 * 视频转换最大尺寸宽度
	 */
	public static int V_BIG_WIDTH;
	/**
	 * 视频转换最大尺寸高度
	 */
	public static int V_BIG_HEIGHT;
	

	//以下三个参数为固定变量不进行修改
	/**
	 * 视频文件上传后统一转码格式
	 */
	public static String VIDEO_FORMAT_TYPE = "mp4";

	/**
	 * 音频文件上传后统一转码格式
	 */
	public static String AUDIO_FORMAT_TYPE= "mp3";

	/**
	 * 图片文件上传后统一转码格式
	 */
	public static String IMG_FORMAT_TYPE= "png";
}
  
	